package org.kernely.mail;

import java.util.HashSet;
import java.util.Set;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.kernely.core.service.KernelyBundleService;
import org.kernely.mail.administration.KernelyMailAdminPresenter;
import org.kernely.mail.administration.KernelyMailAdminView;
import org.kernely.security.service.KernelySecurityService;
import org.kernely.ui.mvp.IKernelyView;
import org.kernely.ui.mvp.KernelyEventBus;
import org.kernely.ui.mvp.KernelyViewPresenterManager;
import org.kernely.ui.mvp.ViewIdentifier;
import org.kernely.ui.services.KernelyUIBundle;
import org.kernely.ui.services.KernelyUIBundleService;
import org.kernely.ui.utils.KernelyBundleMenu;

import com.vaadin.terminal.ExternalResource;
import com.vaadin.terminal.Resource;

public class MailBundle implements KernelyUIBundle {

	/**
	 * The UI registering service
	 */
	private static KernelyUIBundleService uiBundleService;

	/**
	 * The security service : retrieving current user, roles...
	 */
	private static KernelySecurityService securityService;
		
	
	/**
	 * Gets the security service.
	 * @return the security service.
	 */
	public static KernelySecurityService getSecurityService() {
		return securityService;
	}

	/**
	 * Get the event bus, to send UI events.
	 * @return The event bus.
	 */
	public static KernelyEventBus getEventBus() {
		Session session = (Session) SecurityUtils.getSubject().getSession();
		return (KernelyEventBus) session.getAttribute("eventBus");
		
	}
	
	/**
	 * Method called by the Service Component Runtime
	 * 
	 * @param service
	 *            : the KernelyBundleService needed to register this bundle
	 */
	public final void setModuleService(KernelyBundleService service) {
		// Registering the bundle
		service.registerModule(this);
	}

	/**
	 * Method called by the Service Component Runtime
	 * 
	 * @param service
	 *            : the KernelyBundleService needed to unregister this bundle
	 */
	public final void unsetModuleService(KernelyBundleService service) {
		// Unregistering the bundle
		service.unregisterModule(this);
	}

	/**
	 * Sets the KernelyUIBundleService Declarative Service.
	 * 
	 * @param uiBundleService
	 *            : the KernelyUIBundleService service
	 */
	public final void setUIBundleService(KernelyUIBundleService uiBundleService) {
		MailBundle.uiBundleService = uiBundleService;
		MailBundle.uiBundleService.registerModule(this);
	}

	/**
	 * Unsets the KernelyUIBundleService Declarative Service.
	 * 
	 * @param uiBundleService
	 *            : the KernelyUIBundleService service
	 */
	public final void unsetUIBundleService(KernelyUIBundleService service) {
		MailBundle.uiBundleService.unregisterModule(this);
	}
	
	/**
	 * Sets the security Declarative Service
	 * @param service The security service.
	 */
	public final void setSecurityService(KernelySecurityService service) {
		securityService = service;
	}

	/**
	 * Unsets the security Declarative Service
	 * @param service The security service.
	 */
	public final void unsetSecurityService(KernelySecurityService service) {}


	/**
	 * Get the administration view of the stream bundle.
	 */
	public final Class<? extends IKernelyView> getAdministrationView() {
		return KernelyMailAdminView.class;
	}

	/**
	 * @see org.kernely.ui.services.KernelyUIBundle#getMenu()
	 */
	public final KernelyBundleMenu getMenu() {
		KernelyBundleMenu menu = new KernelyBundleMenu();
		
		return menu;
	}


	/**
	 * @see org.kernely.ui.services.KernelyUIBundle#getViewPresenter()
	 */
	public final KernelyViewPresenterManager getViewPresenter() {
		KernelyViewPresenterManager hm = new KernelyViewPresenterManager();
		// Adding all associations View - Presenter
		hm.put(KernelyMailAdminView.class, KernelyMailAdminPresenter.class);
		return hm;
	}

	/**
	 * Return the name of the stream bundle.
	 * @return the name of the bundle.
	 */
	public final String getName() {
		return "Mail Bundle";
	}
	
	/**
	 * @see org.kernely.ui.mvp.KernelyUIBundle#getIcon()
	 */
	public final Resource getIcon() {
		return new ExternalResource("mailbundle/images/mail32.png");
	}


	/**
	 * @see org.kernely.ui.services.KernelyUIBundle#getFrontPages()
	 */
	public Set<ViewIdentifier> getFrontPages() {
		// Create the set
		HashSet<ViewIdentifier> views = new HashSet<ViewIdentifier>();
		// Add the view
		
		return views;
	}
}
